package ecode

import (
	"encoding/json"
	"fmt"
)

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func New(code int, msg string) error {
	return &Response{
		Code: code,
		Msg:  msg,
	}
}

func (r *Response) Error() string {
	e, _ := json.Marshal(r)
	return string(e)
}

func Wrap(err error, msg string) error {
	if e, ok := err.(*Response); ok {
		return &Response{
			Code: e.Code,
			Msg:  e.Msg + " : " + msg,
		}
	}
	return fmt.Errorf(err.Error() + " : " + msg)
}

var (
	ERROK                = New(0, "ok")
	ERRParam             = New(1, "param error")
	ERRDB                = New(2, "db error")
	ERRRedis             = New(3, "redis error")
	ERRSYSTEM            = New(4, "system error")
	ERRLogin             = New(5, "login failed")
	ErrLoginNotReg       = New(6, "not register")
	ErrLoginGenKey       = New(7, "generate token fail")
	ERRRecordNotFound    = New(8, "record not found")
	ERRPermissionDeny    = New(9, "unauthorized operation")
	ERRFileNotExist      = New(10, "file not exist")
	ERRToken             = New(11, "token error")
	ERRTokenExpired      = New(12, "token has expired")
	ERRCertNotCorrect    = New(13, "错误的证书")
	ERRNetDisconnect     = New(14, "无法连接，请检查网络")
	ERRLoginUserNotFound = New(15, "账号错误")
	ERRLoginPwdNotFound  = New(16, "密码错误")
	ERRDBKeyConflict     = New(17, "数据库存在重复key值")
	ERRLicenseExpire     = New(18, "证书已过期，请联系服务提供商")
	ERRLicenseNotExist   = New(19, "证书不存在或非法，请确认")
	ERRLicenseLimit      = New(20, "证书个数限制，请确认")
	ERRCaptChaFetch      = New(21, "验证码获取失败")
	ERRCaptChaVerify     = New(22, "验证码错误")
	//ERRTelnetCreateError = New(9, "telnet创建失败")
)
